<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">

                <el-form-item label="充值模板" prop="t_id">
                    <el-select class="w-[280px]" clearable v-model="queryParams.t_id">
                        <el-option
                            v-for="item in optionsData.articleCate"
                            :key="item.t_id"
                            :label="item.title"
                            :value="item.t_id"
                        />
                    </el-select>
                </el-form-item>

                <el-form-item label="充值类型" prop="type">
                    <el-select
                        v-model="queryParams.type"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="全部" value="-1" />
                        <el-option label="普通充值" value="1" />
                        <el-option label="会员充值" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select
                        v-model="queryParams.status"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="全选" value="-1" />
                        <el-option label="显示" value="1" />
                        <el-option label="隐藏" value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['client:gear:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="充值编号" prop="id" min-width="100" />
                <el-table-column label="充值类型" prop="type" min-width="100">
                    <template #default="{ row }">
                        <el-tag :type="row.type === 1 ? 'success' : 'danger'">
                            {{ row.type === 1 ? '普通充值' : '会员充值' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="标题" prop="title" min-width="100" />
                <el-table-column label="充值金额" prop="price" min-width="100" />
                <el-table-column label="币-购买" prop="buy" min-width="100" />
                <el-table-column label="币-赠送" prop="gift" min-width="100" />
                <el-table-column label="会员天数" prop="vip_days" min-width="100" />
                <el-table-column label="充值次数" prop="recharge_num" min-width="100">
                    <template #default="{ row }">
                        <span>{{ row.recharge_num !== 0 ? row.recharge_num : '不限' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="排序" prop="sort" min-width="100" />
                <el-table-column label="状态" prop="status" min-width="100">
                    <template #default="{ row }">
                        <el-tag :type="row.status === 1 ? 'success' : 'danger'">
                            {{ row.status === 1 ? '显示' : '隐藏' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['client:gear:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['client:gear:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="gear">
import { rechargeGearDelete, rechargeGearLists } from '@/api/client/recharge_gear'
import { drechargeGearTypeAll } from '@/api/client/recharge_gear_type'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import { useDictOptions } from '@/hooks/useDictOptions'
const { query } = useRoute()
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    t_id: Number(query?.t_id || 0),
    type: '-1',
    title: '',
    status: '-1',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: rechargeGearLists,
    params: queryParams
})

// 存储支付配置数据
const { optionsData } = useDictOptions<{
    articleCate: any[]
}>({
    articleCate: {
        api: drechargeGearTypeAll
    }
})


const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add',{t_id: queryParams.t_id})
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await rechargeGearDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
